package com.ruoyi.lhky.controller;

import com.ruoyi.lhky.pojo.User;
import com.ruoyi.lhky.service.UserService;
import com.ruoyi.lhky.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理", description = "用户相关操作")
public class UserAppController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "注册成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误")
    })
    public R register(@RequestBody User user) {
        System.out.println(user);
        return userService.register(user);
    }

    @PostMapping("/checkAccount")
    @Operation(summary = "检查账号是否存在", description = "检查账号是否已被注册")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "账号已存在"),
            @ApiResponse(responseCode = "404", description = "账号不存在")
    })
    public R checkAccount(@RequestBody User user) {
        User existingUser = userService.findByAccount(user.getAccount());
        if (existingUser != null) {
            return new R(true);
        } else {
            return new R(false);
        }
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "登录成功"),
            @ApiResponse(responseCode = "401", description = "用户名或密码错误")
    })
    public R login(@RequestBody User user) {
        return userService.login(user);
    }

    @GetMapping("/getUser")
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户信息及工作单位名称")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "用户不存在")
    })
    public R getUser(@Parameter(description = "用户ID") @RequestParam Long id) {
        User user = userService.getUserById(id);
        if (user != null) {
            return new R(true, user);
        } else {
            return new R(false, "用户不存在");
        }
    }

    @PostMapping("/changePassword")
    @Operation(summary = "修改密码", description = "修改用户密码接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "密码修改成功"),
            @ApiResponse(responseCode = "400", description = "请求参数错误")
    })
    public R changePassword(@RequestBody Map<String, String> passwordData) {
        String account = passwordData.get("account");
        String currentPassword = passwordData.get("currentPassword");
        String newPassword = passwordData.get("newPassword");
        return userService.changePassword(account, currentPassword, newPassword);
    }

}
